import React, { Component } from 'react';
import { connect } from 'dva';
import { Card, Row, Col, Button, Collapse, message, InputNumber, Icon, Modal } from 'antd';
import Factory from '../../factory';
import numeral from 'numeral';
import dataEditCreate from '../../addons/DataEdit'
import DataEditTable from '../../addons/DataEditTable'
const daoName = "BaseEvaCalc";
const { Panel } = Collapse;
const renderNumber = (num) => {
  let value = ''
  if (num) {
    value = numeral(num).format('0,0.00')
  } else {
    value = '0'
  }
  return value
};
const EditFrom = dataEditCreate(daoName);
class evaCalc3Form extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false,
      qxValue: "",
      params: {},
      zhsyl: 0,
      czll: 0,
    };
  }
  componentDidMount() {
  };
  ZhsylCalcHandler() {
    let czll = 0
    let ftp = 0
    let ckrj = 0
    let fparj = 0
    let ckftp = 0
    let ckll = 0
    let zcqx = 0
    let ckqx = 0
    let zs = 0
    const formzhsyl = this.refs.formzhsyl.getForm();
    formzhsyl.validateFields((err, values) => {
      if (!err) {
        czll = Number(values.czll)
        ftp = Number(values.ftp)
        ckrj = Number(values.ckrj)
        fparj = Number(values.fparj)
        ckftp = Number(values.ckftp)
        ckll = Number(values.ckll)
        zcqx = Number(values.zcqx)
        ckqx = Number(values.ckqx)
        zs = Number(values.zs)
      }
    })
    let field = czll / 1.06 - ftp + ckrj / fparj * (ckftp - ckll) / zcqx * ckqx + zs / fparj
    this.setState({ zhsyl: field.toFixed(2) })
  }
  ZhsylClearHandler() {
    const formzhsyl = this.refs.formzhsyl.getForm();
    formzhsyl.resetFields()
    this.setState({ zhsyl: 0 })
  }
  CzllCalcHandler() {
    let zhsyl = 0
    let ftp = 0
    let ckrj = 0
    let fparj = 0
    let ckftp = 0
    let ckll = 0
    let zcqx = 0
    let ckqx = 0
    let zs = 0
    const formczll = this.refs.formczll.getForm();
    formczll.validateFields((err, values) => {
      if (!err) {
        zhsyl = Number(values.zhsyl)
        ftp = Number(values.ftp)
        ckrj = Number(values.ckrj)
        fparj = Number(values.fparj)
        ckftp = Number(values.ckftp)
        ckll = Number(values.ckll)
        zcqx = Number(values.zcqx)
        ckqx = Number(values.ckqx)
        zs = Number(values.zs)
      }
    })
    let field = (zhsyl - ckrj / fparj * (ckftp - ckll) / zcqx * ckqx - zs / fparj + ftp) * 1.06
    this.setState({ czll: field.toFixed(2) })
  }
  czllClearHandler() {
    const formczll = this.refs.formczll.getForm();
    formczll.resetFields()
    this.setState({ czll: 0 })
  }
  render() {
    let ZHSYL_CONTROLS = [
      { name: 'czll', required: true, type: 'number', label: '出账利率(%)', grid: 12 / 24 },
      { name: 'ftp', required: true, type: 'number', label: '资产业务FTP(%)', grid: 12 / 24 },
      { name: 'ckrj', required: true, type: 'number', label: '存款日均(万)', grid: 12 / 24 },
      { name: 'fparj', required: true, type: 'number', label: 'FPA日均(万)', grid: 12 / 24 },
      { name: 'ckftp', required: true, type: 'number', label: '存款FTP(%)', grid: 12 / 24 },
      { name: 'ckll', required: true, type: 'number', label: '存款利率(%)', grid: 12 / 24 },
      { name: 'zcqx', required: true, type: 'number', label: '资产业务期限(天)', grid: 12 / 24 },
      { name: 'ckqx', required: true, type: 'number', label: '存款期限(天)', grid: 12 / 24 },
      { name: 'zs', required: true, type: 'number', label: '中收(万)', grid: 12 / 24 },
    ]
    let CZLL_CONTROLS = [
      { name: 'zhsyl', required: true, type: 'number', label: '综合收益率(%)', grid: 12 / 24 },
      { name: 'ftp', required: true, type: 'number', label: '资产业务FTP(%)', grid: 12 / 24 },
      { name: 'ckrj', required: true, type: 'number', label: '存款日均(万)', grid: 12 / 24 },
      { name: 'fparj', required: true, type: 'number', label: 'FPA日均(万)', grid: 12 / 24 },
      { name: 'ckftp', required: true, type: 'number', label: '存款FTP(%)', grid: 12 / 24 },
      { name: 'ckll', required: true, type: 'number', label: '存款利率(%)', grid: 12 / 24 },
      { name: 'zcqx', required: true, type: 'number', label: '资产业务期限(天)', grid: 12 / 24 },
      { name: 'ckqx', required: true, type: 'number', label: '存款期限(天)', grid: 12 / 24 },
      { name: 'zs', required: true, type: 'number', label: '中收(万)', grid: 12 / 24 },
    ]
    return <div style={{ background: '#ECECEC', overflowY: "scroll", marginTop: -16, padding: '5px 5px' }}>
      <Collapse accordion defaultActiveKey={['1', '2']}>
        <Panel header="综合收益率" key="1">
          <EditFrom ref="formzhsyl" daoName={daoName} controls={ZHSYL_CONTROLS} />
          <div>
            <Button type="Primary" style={{ margin: 5 }} onClick={this.ZhsylCalcHandler.bind(this)}>
              计算
            </Button>
            <Button type="Primary" onClick={this.ZhsylClearHandler.bind(this)}>
              <Icon type="retweet" />重置
            </Button>

            <span>
              {this.state.zhsyl !== 0 ? <span style={{ marginLeft: 30 }}>综合收益率：{renderNumber(this.state.zhsyl)}%</span> : null}
            </span>
            <div style={{ marginTop: 10 }}>
              <span style={{ marginLeft: 30 }}>
                公式：综合收益率 = 资产业务出账利率/1.06 - 资产业务FTP +存款日均/FPA日均 *（存款FTP-存款利率）/ 资产业务期限 * 存款期限 + 中收/FPA日均
              </span>
            </div>
          </div>
        </Panel>
        <Panel header="资产业务出账利率" key="2">
          <div>
            <EditFrom ref="formczll" daoName={daoName} controls={CZLL_CONTROLS} selectFirst={false} />
          </div>
          <div>
            <Button type="Primary" style={{ margin: 5 }} onClick={this.CzllCalcHandler.bind(this)}>
              计算
            </Button>
            <Button type="Primary" onClick={this.czllClearHandler.bind(this)}>
              <Icon type="retweet" />重置
            </Button>
            <span>
              {this.state.czll !== 0 ? <span style={{ marginLeft: 30 }}>资产业务出账利率：{renderNumber(this.state.czll)}%</span> : null}
            </span>
            <div style={{ marginTop: 10 }}>
              <span style={{ marginLeft: 30 }}>
                公式：资产业务出账利率 = （综合收益率 - 存款日均/FPA日均 *（存款FTP-存款利率）/资产业务期限 * 存款期限 - 中收/FPA日均 + 资产业务FTP）*1.06
              </span>
            </div>
          </div>
        </Panel>
      </Collapse>
    </div>
  }
}
function mapStateToProps(state) {
  const { params, results } = state.BaseEvaCalc;
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    params, results
  }
}
evaCalc3Form = connect(mapStateToProps)(evaCalc3Form)
export default evaCalc3Form;

let render = (props) => React.createElement(evaCalc3Form, props);
let f = new Factory()
f.set("evaCalc3Form", render)
